<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hadd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree
          default-expand-all=""
          :data="list"
        >
          <!-- 使用作用域插槽获取组件内部的数据 -->
          <!-- el-tree提供的默认的作用域插槽 #default="{data}" 获取el-tree组件内部的数据-->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hadd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="doEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hdel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>

        </el-tree>
      </el-card>
      <!-- 准备新增子部门或编辑部门的弹层 -->
      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit ? '编辑部门' : '新增子部门'"
        :visible.sync="showDialog"
        width="50%"
        @close="close"
      >
        <!-- 把id传给子组件 -->
        <AddorEdit
          v-if="showDialog"
          :id="curId"
          ref="deptRef"
          :is-edit="isEdit"
          :orign-list="orignList"
          @close="showDialog=false"
          @success="hsuccess"
        />
        <!-- <span>这是一段信息</span> -->
        <!-- <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="showDialog = false">确 定</el-button>
        </span> -->
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartment, delDepartment } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [], // 存储部门列表
      showDialog: false, // 控制弹层显示和隐藏
      curId: '', // 当做部门的id，作为子部门的pid
      isEdit: false, //  false 是 新增   true 是 编辑  通过事件把编辑或新增的状态给到子组件
      orignList: []
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    // 只要关闭弹层就调用子组件的方法
    close() {
      this.$refs.deptRef.resetForm()
    },
    hdel(id) {
      this.$confirm('确定删除吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartment(id)
        if (res.code === 10000) {
          this.$message.success(res.message)
          this.loadDepartment()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 编辑
    doEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
    },
    hsuccess() {
      this.loadDepartment()
      this.showDialog = false
    },
    // 在添加子部门上添加事件传入id 这里接收并存入state 然后在传给子组件
    hadd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    async loadDepartment() {
      try {
        const res = await getDepartment()
        // console.log(res)
        if (res.code !== 10000) this.$message.error(res.message)
        // 表单的非重校验 把每一个部门的id, code, pid, name 传递给子组件，
        // 想要拿到每一项，所以就先遍历 并 返回对象，把所有的对象添加到数组中
        this.orignList = res.data.depts.map(item => {
          const { id, code, pid, name } = item
          return { id, code, pid, name }
        })
        this.$message.success(res.message)
        res.data.depts.shift()
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
